$(function () {
    $('#btn-removeAll').on('click', function (e) {
    	var cacheType = $("#cacheType").val();
	    	if(cacheType==null||cacheType==""){
	    	    BootstrapDialog.show({
	                title: '提示',
	                message: '缓存类型 不能为空',
	                buttons: [{
     	                 label: '确定',
     	                 hotkey: 65, // Keycode of keyup event of key 'A' is 65.
     	                 action: function(dialogItself) {
     	                	 dialogItself.close();
     	                 }
                         }]
	            });
	    	    return ;
	    	}
	    	 BootstrapDialog.show({
	             title: '确认',
	             message: '是否清除该缓存类型下的所有缓存?',
	             onshow: function(dialogItself) {
	             },
	             buttons: [{
	                 label: '确定',
	                 hotkey: 65, // Keycode of keyup event of key 'A' is 65.
	                 action: function(dialogItself) {
	                	 $.ajax({
	         	            type: "DELETE",
	         	            url: "/cache/removeAll/"+cacheType,
	         	            success: function (data) {
	         	            	if(data==true){
	         	            		 BootstrapDialog.show({
	         	                        title: '提示',
	         	                        message: '删除成功',
	         	                         buttons: [{
	         	     	                 label: '确定',
	         	     	                 hotkey: 65, // Keycode of keyup event of key 'A' is 65.
	         	     	                 action: function(dialogItself) {
	         	     	                	 dialogItself.close();
	         	     	                 }
	         	                         }]
	         	                    });
	         	            		 dialogItself.close();
	         	            	}
	         	            }
	         	        });
	                 }
	             }, {
	                 label: '取消',
	                 hotkey: 66,
	                 action: function(dialogItself) {
	                	 dialogItself.close();
	                 }
	             }]
	         });
    });
    $('.selectpicker').on('change', function (e) {
       console.log($("#cacheType").val());
    });
    $('#doSearch').on('click', function (e) {
    	var cacheType = $("#cacheType").val();
    	var cacheKey = $("#cacheKey").val();
    	if(cacheType==null||cacheType==""||cacheKey==null||cacheKey==""){
    		BootstrapDialog.show({
                title: '提示',
                message: '缓存类型|缓存key  不能为空',
                buttons: [{
 	                 label: '确定',
 	                 hotkey: 65, // Keycode of keyup event of key 'A' is 65.
 	                 action: function(dialogItself) {
 	                	 dialogItself.close();
 	                	 $('#privew').empty();
 	                 }
                     }]
            });
    	}else{
            $.ajax({
                type: "GET",
                url: "/cache/"+cacheType+"/"+cacheKey,
                success: function (data) {
                	$("#privew").JSONView(data);
                }
            });
    	}
    });
    $('#btn-remove').on('click', function (e) {
    	var cacheType = $("#cacheType").val();
    	var cacheKey = $("#cacheKey").val();
	    	if(cacheType==null||cacheType==""||cacheKey==null||cacheKey==""){
	    	    BootstrapDialog.show({
	                title: '提示',
	                message: '缓存类型|缓存key 不能为空'
	            });
	    	    return ;
	    	}
	    	 BootstrapDialog.show({
	             title: '确认',
	             message: '是否确认删除?',
	             onshow: function(dialog) {
	             },
	             buttons: [{
	                 label: '确定',
	                 hotkey: 65, // Keycode of keyup event of key 'A' is 65.
	                 action: function(dialogItself) {
	                	 $.ajax({
	         	            type: "DELETE",
	         	            url: "/cache/remove/"+cacheType+"/"+cacheKey,
	         	            success: function (data) {
	         	            	if(data==true){
	         	            		 BootstrapDialog.show({
	         	                        title: '提示',
	         	                        message: '删除成功',
	         	                       buttons: [{
		         	     	                 label: '确定',
		         	     	                 hotkey: 65, // Keycode of keyup event of key 'A' is 65.
		         	     	                 action: function(dialogItself) {
		         	     	                	 dialogItself.close();
		         	     	                 }
		         	                         }]
	         	                    });
	         	            		 dialogItself.close();
	         	            		 $('#privew').empty();
	         	            	}
	         	            }
	         	        });
	                 }
	             }, {
	                 label: '取消',
	                 hotkey: 66,
	                 action: function(dialogItself) {
	                	 dialogItself.close();
	                 }
	             }]
	         });
    });
    $('#btn-delay').on('click', function (e) {
        if ($('#expireHour').val() == "") {
            alert("Please input the number!");
        }
        if($('#expireTime').text()==""){
            $('#updateModal').modal('hide');
            return;
        }
        $.ajax({
            type: 'PUT',
            url: "/cache/" + $('#key').text(),
            data: "hour=" + $('#expireHour').val(),
            success: function (data) {
                show();
                init();
                $('#updateModal').modal('hide');
            }
        });
    });
    init();
});
function clear() {
    $('#key').empty();
    $('#expireTime').empty();
    $('#desc').empty();
    $('#privew').empty();
}
function show() {
    $('#alert').show();
    setTimeout(function () {
        $("#alert").hide()
    }, 1000);
}
function init() {
    clear();
    $.ajax({
        type: "GET",
        url: "/cache/list/caches",
        success: function (defaultData) {
//        	console.log(defaultData);
			   $.each(defaultData, function(index, value) {
				   $('.selectpicker').append(
						   "<option value='"+value+"'>"+value+"</option>"
		          );
		        });
			   $('.selectpicker').selectpicker('refresh');
        }
    });
}